﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYFamilyLibDataManager;

namespace HYFamilyLibManager.FamilyLibModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdUpdate : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			try
			{
				if (MessageBox.Show("升级族库将要花费将近1-2个小时的时间，是否继续执行？", "族库升级", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
				{
					DlgProgress dlgProgress = new DlgProgress(new List<FamilyLibDatabase>
					{
						FamilyLibDatabase.GetSqliteDB(SubjectType.Architecture, (Convert.ToInt32(this.m_uiApp.Application.VersionNumber) - 1).ToString()),
						FamilyLibDatabase.GetSqliteDB(SubjectType.Electric, (Convert.ToInt32(this.m_uiApp.Application.VersionNumber) - 1).ToString()),
						FamilyLibDatabase.GetSqliteDB(SubjectType.HVAC, (Convert.ToInt32(this.m_uiApp.Application.VersionNumber) - 1).ToString()),
						FamilyLibDatabase.GetSqliteDB(SubjectType.Plumbing, (Convert.ToInt32(this.m_uiApp.Application.VersionNumber) - 1).ToString()),
						FamilyLibDatabase.GetSqliteDB(SubjectType.System, (Convert.ToInt32(this.m_uiApp.Application.VersionNumber) - 1).ToString())
					}, this.m_uiApp);
					Process currentProcess = Process.GetCurrentProcess();
					if (currentProcess != null)
					{
						HYFamilyLibManager.WindowHandle owner = new HYFamilyLibManager.WindowHandle(currentProcess.MainWindowHandle);
						dlgProgress.ShowDialog(owner);
					}
				}
			}
			catch
			{
				MessageBox.Show("没有检测到上一版本族库。");
			}
			return base.YJKExcute(cmdData, ref message, elements);
		}
	}
}
